package com.jzy.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzy.common.to.es.BrandEsTo;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.Query;
import com.jzy.common.utils.R;
import com.jzy.gulimall.product.dao.BrandDao;
import com.jzy.gulimall.product.entity.BrandEntity;
import com.jzy.gulimall.product.entity.SkuInfoEntity;
import com.jzy.gulimall.product.feign.SearchFeignService;
import com.jzy.gulimall.product.service.BrandService;
import com.jzy.gulimall.product.service.CategoryBrandRelationService;
import com.jzy.gulimall.product.service.SkuInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    private SearchFeignService searchFeignService;

    @Resource
    private SkuInfoService skuInfoService;
    /**
     * 品牌列表 模糊匹配
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 修改品牌表 更新其他关联表中的数据
     * 修改品牌表 更新es中的数据
     */
    @Override
    @Transactional
    public void updateDetailById(BrandEntity brand) {
        this.updateById(brand);
        //同步更新其他关联表中的数据
        categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        //更新es中的品牌数据
        BrandEsTo brandEsTo = new BrandEsTo();
        brandEsTo.setBrandId(brand.getBrandId());
        brandEsTo.setBrandName(brand.getName());
        brandEsTo.setBrandImg(brand.getLogo());
        List<SkuInfoEntity> skuInfoEntities = skuInfoService.list(new QueryWrapper<SkuInfoEntity>().eq("brand_id", brand.getBrandId()));
        List<Long> skuIds = skuInfoEntities.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());
        brandEsTo.setSkuIds(skuIds);
        R r = searchFeignService.updateEsBrand(brandEsTo);
        if (r.getCode() != 0) {
            log.error("更新es品牌数据失败");
        }
        // TODO: 2020/9/7 更新其他关联表中的冗余信息
    }

    @Override
    public List<BrandEntity> getBrandsByIds(List<Long> brandIds) {
        return baseMapper.selectList(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
    }

}